package com.mycharx.springboothibernate.common.key;

import com.mycharx.springboothibernate.common.SpringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * 雪花算法key生成器
 *
 * @author 张卜亢
 * @date 2020.03.29 01:13:04
 */
public class SnowFlakeKeyGen implements IdentifierGenerator {

    private SnowFlake snowFlake;

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        if (snowFlake == null) {
            snowFlake = SpringUtils.getBean(SnowFlake.class);
        }
        return snowFlake.nextId();
    }
}